<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
use dh2y\qrcode\QRcode;

/**
 * @ControllerAnnotation(title="商户授权列表")
 */
class Authorize extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Merchant();
        
    }

     /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->get();
            //车辆id
            $uid = $param['uid'];
            
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                // foreach ($list as $k => $v){
                     
                 
                //     $list[$k] = $v;
                // }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        } else {
            $param = $this->request->get();
            $this->assign('uid', $param['id']);
            return $this->fetch();
        }
  
    }

       /**
    * @NodeAnotation(title="授权")
    */
    public function addsq()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['mid'])){
                $this->error('商户id不能为空');
            }
            if(empty($post['vid'])){
                $this->error('参数错误');
            }
            $flag = Db::name("merchant")->where("id",$post["mid"])->find();
            $user = Db::name("user")->where("merchant_id",$post["mid"])->find();
            $vehicle_sq = Db::name("vehicle_sq")->where("id",$post["vid"])->find();
            if(!empty($vehicle_sq)){
                unset($vehicle_sq["id"]);
                unset($vehicle_sq["reason"]);
                $vehicle_sq["create_time"]=time();
                $vehicle_sq["merchant_id"] = $post["mid"];
                $vehicle_sq["city"] = $flag["city"];
                $vehicle_sq["uid"] = $user["id"];
                Db::name("vehicle")->insert($vehicle_sq);
                $vehicle_id =  Db::name('vehicle')->getLastInsID();
                $vehicle_bystages_sq = Db::name("vehicle_bystages_sq")->where("vehicle_id",$post["vid"])->select()->toArray();
                foreach($vehicle_bystages_sq as $k=>$v){
                    $datv = [
                        "vehicle_id"=>$vehicle_id,
                        "number_ofperiods"=>$v["number_ofperiods"],
                        "monthly_rentalamount"=>$v["monthly_rentalamount"]
                    ];
                    Db::name("vehicle_bystages")->insert($datv);
               }
            }else{
                $this->error('车辆不存在');
            }
            $this->success('操作成功');
        }
        
    }

 
 
     
 
    
    
}